<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="卡号" prop="userCard">
        <el-input
          v-model="queryParams.userCard"
          placeholder="请输入卡号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="患者姓名" prop="patientName">
        <el-input
          v-model="queryParams.patientName"
          placeholder="请输入患者姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="患者id" prop="patientId">
        <el-input
          v-model="queryParams.patientId"
          placeholder="请输入患者id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="手机号" prop="patientTel">
        <el-input
          v-model="queryParams.patientTel"
          placeholder="请输入支付手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单据号" prop="receiptNo">
        <el-input
          v-model="queryParams.receiptNo"
          placeholder="请输入单据号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="支付类型"
          clearable
          style="width: 190px"
        >
          <el-option
            v-for="dict in typeList"
            :key="dict.type"
            :label="dict.name"
            :value="dict.type"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:order:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:order:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:order:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:order:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row> -->

    <el-table
      v-loading="loading"
      :data="orderList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="订单号" align="center" prop="orderNo" />
      <el-table-column label="卡号" align="center" prop="userCard" />
      <!-- <el-table-column label="卡类型" align="center" prop="userCardType" /> -->
      <el-table-column label="患者姓名" align="center" prop="patientName" />
      <el-table-column label="患者id" align="center" prop="patientId" />
      <el-table-column label="患者类型" align="center" prop="patientType">
        <template slot-scope="scope">
          <span v-if="scope.row.patientType == 1">门诊</span>
          <span v-if="scope.row.patientType == 2">住院</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="patientTel" />
      <el-table-column label="充值金额" align="center" prop="money" />
      <el-table-column label="类型 " align="center" prop="type">
        <template slot-scope="scope">
          <span v-if="scope.row.type == 1">门诊充值</span>
          <span v-if="scope.row.type == 2">预充值</span>
          <span v-if="scope.row.type == 3">缴费处方单</span>
          <span v-if="scope.row.type == 4">挂号</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="充值标题" align="center" prop="payTitle" /> -->
      <el-table-column label="支付方式 " align="center" prop="payType">
        <template slot-scope="scope">
          <span v-if="scope.row.payType == 0">余额</span>
          <span v-if="scope.row.payType == 1">微信</span>
          <span v-if="scope.row.payType == 2">支付宝</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="payStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.payStatus == 0">待支付</span>
          <span v-if="scope.row.payStatus == 1">已支付</span>
          <span v-if="scope.row.payStatus == 2">作废</span>
          <span v-if="scope.row.payStatus == 3">异常</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="his支付结果" align="center" prop="payMsg" /> -->
      <!-- <el-table-column label="扩展字段" align="center" prop="ext" /> -->
      <el-table-column label="单据号" align="center" prop="receiptNo" />
      <el-table-column
        label="支付时间"
        align="center"
        prop="payTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:order:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:order:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单充值对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单id" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单id" />
        </el-form-item>
        <el-form-item label="订单商户号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单商户号" />
        </el-form-item>
        <el-form-item label="userId" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入userId" />
        </el-form-item>
        <el-form-item label="卡号" prop="userCard">
          <el-input v-model="form.userCard" placeholder="请输入卡号" />
        </el-form-item>
        <el-form-item label="患者姓名" prop="patientName">
          <el-input v-model="form.patientName" placeholder="请输入患者姓名" />
        </el-form-item>
        <el-form-item label="患者id" prop="patientId">
          <el-input v-model="form.patientId" placeholder="请输入患者id" />
        </el-form-item>
        <el-form-item label="支付手机号" prop="patientTel">
          <el-input v-model="form.patientTel" placeholder="请输入支付手机号" />
        </el-form-item>
        <el-form-item label="money 充值金额" prop="money">
          <el-input v-model="form.money" placeholder="请输入money 充值金额" />
        </el-form-item>
        <el-form-item label="充值标题" prop="payTitle">
          <el-input v-model="form.payTitle" placeholder="请输入充值标题" />
        </el-form-item>
        <el-form-item label="his支付结果" prop="payMsg">
          <el-input
            v-model="form.payMsg"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="扩展字段" prop="ext">
          <el-input
            v-model="form.ext"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="单据号" prop="receiptNo">
          <el-input v-model="form.receiptNo" placeholder="请输入单据号" />
        </el-form-item>
        <el-form-item label="支付时间" prop="payTime">
          <el-date-picker
            clearable
            v-model="form.payTime"
            type="date"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择支付时间"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listOrder,
  getOrder,
  delOrder,
  addOrder,
  updateOrder,
} from "@/api/system/order";

export default {
  name: "Order",
  data() {
    return {
      typeList: [
        { name: "门诊充值", type: 1 },
        { name: "预充值", type: 2 },
        { name: "缴费处方单", type: 3 },
        { name: "挂号", type: 4 },
      ],
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单充值表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 日期范围
      dateRange: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        orderNo: null,
        userId: null,
        userCard: null,
        userCardType: null,
        patientName: null,
        patientId: null,
        patientType: null,
        patientTel: null,
        money: null,
        type: null,
        payTitle: null,
        payType: null,
        payStatus: null,
        payMsg: null,
        ext: null,
        receiptNo: null,
        payTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [{ required: true, message: "$comment不能为空", trigger: "blur" }],
        orderId: [
          { required: true, message: "订单id不能为空", trigger: "blur" },
        ],
        orderNo: [
          { required: true, message: "订单商户号不能为空", trigger: "blur" },
        ],
        userId: [
          { required: true, message: "userId不能为空", trigger: "blur" },
        ],
        userCard: [
          { required: true, message: "卡号不能为空", trigger: "blur" },
        ],
        userCardType: [
          { required: true, message: "卡类型不能为空", trigger: "change" },
        ],
        patientName: [
          { required: true, message: "患者姓名不能为空", trigger: "blur" },
        ],
        patientId: [
          { required: true, message: "患者id不能为空", trigger: "blur" },
        ],
        patientType: [
          {
            required: true,
            message: "患者类型 1是门诊 2是住院不能为空",
            trigger: "change",
          },
        ],
        patientTel: [
          { required: true, message: "支付手机号不能为空", trigger: "blur" },
        ],
        money: [
          {
            required: true,
            message: "money 充值金额不能为空",
            trigger: "blur",
          },
        ],
        type: [
          {
            required: true,
            message: "类型 1是门诊充值 2是预充值 3缴费处方单 4是挂号不能为空",
            trigger: "change",
          },
        ],
        payTitle: [
          { required: true, message: "充值标题不能为空", trigger: "blur" },
        ],
        payType: [
          {
            required: true,
            message: "来源 0是余额 1是微信 2是支付宝 不能为空",
            trigger: "change",
          },
        ],
        payStatus: [
          {
            required: true,
            message: "状态 0是待支付 1是已支付 2是作废 3是异常不能为空",
            trigger: "blur",
          },
        ],
        payMsg: [
          { required: true, message: "his支付结果不能为空", trigger: "blur" },
        ],
        ext: [{ required: true, message: "扩展字段不能为空", trigger: "blur" }],
        receiptNo: [
          { required: true, message: "单据号不能为空", trigger: "blur" },
        ],
        payTime: [
          { required: true, message: "支付时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单充值列表 */
    getList() {
      this.loading = true;
      listOrder(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.orderList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderId: null,
        orderNo: null,
        userId: null,
        userCard: null,
        userCardType: null,
        patientName: null,
        patientId: null,
        patientType: null,
        patientTel: null,
        money: null,
        type: null,
        payTitle: null,
        payType: null,
        payStatus: "0",
        payMsg: null,
        ext: null,
        receiptNo: null,
        payTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单充值";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getOrder(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单充值";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除订单充值编号为"' + ids + '"的数据项？')
        .then(function () {
          return delOrder(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/order/export",
        {
          ...this.queryParams,
        },
        `order_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
